package com.fjnu.service.impl;

import com.fjnu.dao.UserDao;
import com.fjnu.dao.impl.ArticleDaoImpl;
import com.fjnu.dto.ArticleConditionDto;
import com.fjnu.dto.ArticleDto;
import com.fjnu.entity.Article;
import com.fjnu.service.ArticleService;
import com.fjnu.uitl.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleDaoImpl articleDao;
    @Autowired
    private UserDao userDao;
    @Override
    public PageResult<Article> queryByArticleCondition(ArticleConditionDto articleConditionDto, Integer pageNum, Integer pageSize) {
        return new PageResult<>(articleDao.selectByConditionWithLimit(articleConditionDto,
                (pageNum-1)*pageSize, pageSize).size(),
                articleDao.selectByConditionWithLimit(articleConditionDto,
                        (pageNum-1)*pageSize, pageSize));
    }

    @Override
    public Article queryById(Integer id) {
        return articleDao.selectByUserId( id).get(0);
    }

    @Override
    public Article insertArticle(Article article) {
        articleDao.insert(article);
        ArticleConditionDto articleConditionDto = new ArticleConditionDto();
        articleConditionDto.setUserId(article.getUserId());
        articleConditionDto.setTitle(article.getTitle());
        articleConditionDto.setContent(article.getContent());
        PageResult<Article> articlePageResult = queryByArticleCondition(articleConditionDto, 1, 10);
        return articlePageResult.getRows().get(0);
    }

    @Override
    @Transactional
    public int publishArticle(String articleId, String userId) {
        userDao.updateScore(Integer.parseInt(userId), (long) 10);
        if (articleDao.updateStatus(Integer.parseInt(articleId), 1) != 1){
            throw new RuntimeException("发布文章失败");
        }
        return 1;
    }

    @Override
    @Transactional
    public int publishArticlesByBatch(List<Integer> articleIds, String userId) {
        for (Integer articleId : articleIds) {
            int i = publishArticle(articleId.toString(), userId);
            if (i != 1){
                throw new RuntimeException("发布文章失败");
            }
        }
        return 1;

    }

    @Override
    @Transactional
    public int updateArticle(Integer id,ArticleConditionDto articleConditionDto) {
        if (articleDao.updateByCondition(id,articleConditionDto) != 1){
            throw new RuntimeException("修改文章失败");
        }
        return 1;
    }

    @Override
    @Transactional
    public int removeArticle(String articleId, String userId) {
        if (articleDao.deleteById(Integer.parseInt(articleId)) != 1){
            throw new RuntimeException("删除文章失败");
        }
        if (userDao.updateScore(Integer.parseInt(userId), (long) -10) != 1){
            throw new RuntimeException("删除文章失败");
        }
        return 1;
    }

    @Override
    public List<Article> list(ArticleConditionDto articleConditionDto) {
        return articleDao.list(articleConditionDto);
    }

    @Override
    public int publish(String articleId) {
        return articleDao.updateStatus(Integer.parseInt(articleId), 1);
    }

    @Override
    public int add(ArticleDto articleDto) {
        Article article = new Article();
        article.setUserId(articleDto.getUserId());
        article.setTitle(articleDto.getTitle());
        article.setContent(articleDto.getContent());
        article.setDescription(articleDto.getDescription());
        article.setCreateTime(new java.util.Date());
        article.setOppose(0);
        article.setSupport(0);
        return articleDao.insert(article);
    }
}
